﻿var utils = (function () {
    function StoregeObjectShim() {
        if (!Storage.setObject) {
            Storage.prototype.setObject = function (key, object) {
                this.setItem(key, JSON.stringify(object));
            }
        }

        if (!Storage.getObject) {
            Storage.prototype.getObject = function (key) {
                var objStr = this.getItem(key);
                return JSON.parse(objStr);
            }
        }
    }

    function attachEventHandler(element, eventType, eventHandler) {
        if (!element) {
            return;
        }

        if (document.addEventListener) {
            element.addEventListener(eventType, eventHandler, false);
        }
        else if (document.attachEvent) {
            element.attachEvent("on" + eventType, eventHandler);
        }
        else {
            element['on' + eventType] = eventHandler;
        }
    }

    function StringEscapingShim() {
        String.prototype.escape = function () {
            var tagsToReplace = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;'
            };
            return this.replace(/[&<>]/g, function (tag) {
                return tagsToReplace[tag] || tag;
            });
        };
    }

    function initAllShims() {
        StoregeObjectShim();
        StringEscapingShim();
    }

    return {
        StoregeObjectShim: StoregeObjectShim,
        attachEventHandler: attachEventHandler,
        stringEscapingShim: StringEscapingShim,
        initAllShims: initAllShims,
    }
}());